package com.itheima.web.service.impl.ums;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.service.ums.AdminRoleService;
import com.itheima.service.ums.RoleService;
import com.itheima.vo.ums.AdminRole;
import com.itheima.vo.ums.UmsRole;
import com.itheima.web.mapper.ums.AdminRoleMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class AdminRoleServiceImpl extends ServiceImpl<AdminRoleMapper, AdminRole> implements AdminRoleService {

    @Autowired
    private RoleService roleService;


    @Override
    public void removeByAdminId(Integer adminId) {
        LambdaQueryWrapper<AdminRole> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AdminRole::getAdminId,adminId);
        this.remove(lqw);
    }

    /**
     * 获取指定用户的角色
     * @param adminId
     * @return
     */
    @Override
    public List<UmsRole> getRole(Integer adminId) {
        LambdaQueryWrapper<AdminRole> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AdminRole::getAdminId,adminId);
        List<AdminRole> list = this.list(lqw);

        if(list == null || list.size() == 0){
            return null;
        }
        List<Integer> roleIds = new ArrayList<>();
        for (AdminRole adminRole : list) {
            roleIds.add(adminRole.getRoleId());
        }

        List<UmsRole> umsRoles = roleService.listByIds(roleIds);
        return umsRoles;
    }
}
